package view;

import data.*;
import java.awt.*;
import java.util.List;
import java.util.Vector;

/**
 * 游戏面板绘制视图类
 */
public class GamePanelView {
    // -------------------- view --------------------
    private TankView tankView;
    private BulletView bulletView;
    private BlockView blockView;
    private CampView campView;
    private BombView bombView;
    // -------------------- view --------------------

    public GamePanelView() {
        tankView = new TankView();
        bulletView = new BulletView();
        blockView = new BlockView();
        campView = new CampView();
        bombView = new BombView();
    }

    /**
     * 功能: 绘制游戏面板
     * 参数:
     *   g: Graphics绘图对象
     * 返回值: void
     */
    public void drawGamePanel(Graphics g) {
        GameRuntimeData runtimeData = GameRuntimeData.getInstance();

        // 清空背景
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, 1200, 900);

        // 绘制游戏区域背景
        g.setColor(Color.DARK_GRAY);
        g.fillRect(0, 0, 780, 780);

        // 绘制方块
        for (BlockData block : runtimeData.getBlockList()) {
            blockView.drawBlock(g, block);
        }

        // 绘制大本营
        if (runtimeData.getCamp() != null) {
            campView.drawCamp(g, runtimeData.getCamp());
        }

        // 绘制我方坦克
        if (runtimeData.getHero() != null) {
            tankView.drawTank(g, runtimeData.getHero());

            // 绘制我方子弹
            for (BulletData bullet : runtimeData.getHero().getBulletPool()) {
                bulletView.drawBullet(g, bullet);
            }
        }

        // 绘制敌方坦克
        for (EnemyData enemy : runtimeData.getEnemyPool()) {
            tankView.drawTank(g, enemy);

            // 绘制敌方子弹
            for (BulletData bullet : enemy.getBulletPool()) {
                bulletView.drawBullet(g, bullet);
            }
        }

        // 绘制爆炸效果
        for (BombData bomb : runtimeData.getBombList()) {
            bombView.drawBomb(g, bomb);
        }

        // 绘制游戏信息面板
        drawInfoPanel(g, runtimeData);

        // 绘制边界
        g.setColor(Color.WHITE);
        g.drawRect(0, 0, 779, 779);
    }

    /**
     * 功能: 绘制游戏信息面板
     * 参数:
     *   g: Graphics绘图对象
     *   runtimeData: 游戏数据
     * 返回值: void
     */
    private void drawInfoPanel(Graphics g, GameRuntimeData runtimeData) {
        // 信息面板背景
        g.setColor(Color.GRAY);
        g.fillRect(780, 0, 420, 900);

        // 面板边框
        g.setColor(Color.WHITE);
        g.drawRect(780, 0, 419, 899);

        // 设置字体
        g.setFont(new Font("Arial", Font.BOLD, 16));

        // 绘制标题
        g.setColor(Color.YELLOW);
        g.drawString("坦克大战", 850, 50);

        // 绘制关卡信息
        g.setColor(Color.WHITE);
        g.drawString("关卡: " + runtimeData.getCurrentGrade(), 800, 100);
        g.drawString("得分: " + runtimeData.getScore(), 800, 130);

        // 绘制敌方坦克剩余数量
        int remainingEnemies = runtimeData.getEnemyTotalNum() - runtimeData.getEnemyDeadNum();
        g.drawString("敌方坦克: " + remainingEnemies, 800, 160);

        // 绘制我方坦克信息
        if (runtimeData.getHero() != null) {
            g.drawString("我方血量: " + runtimeData.getHero().getHp(), 800, 190);
            g.drawString("生命数: " + runtimeData.getHero().getLives(), 800, 220);
        }

        // 绘制游戏状态
        String stateText = "";
        switch (runtimeData.getGameState()) {
            case 0: stateText = "按空格开始游戏"; break;
            case 1: stateText = "游戏进行中"; break;
            case 2: stateText = "游戏暂停"; break;
            case 3: stateText = "游戏结束"; break;
            case 4: stateText = "游戏胜利"; break;
        }
        g.setColor(Color.CYAN);
        g.drawString(stateText, 800, 270);

        // 绘制操作说明
        g.setColor(Color.LIGHT_GRAY);
        g.setFont(new Font("Arial", Font.PLAIN, 12));
        g.drawString("操作说明:", 800, 320);
        g.drawString("WASD/方向键: 移动", 800, 340);
        g.drawString("空格键: 射击", 800, 360);
        g.drawString("P键: 暂停/继续", 800, 380);
        g.drawString("R键: 重新开始", 800, 400);

        // 绘制小地图
        drawMiniMap(g, runtimeData);
    }

    /**
     * 功能: 绘制小地图
     * 参数:
     *   g: Graphics绘图对象
     *   runtimeData: 游戏数据
     * 返回值: void
     */
    private void drawMiniMap(Graphics g, GameRuntimeData runtimeData) {
        int mapX = 800;
        int mapY = 450;
        int mapSize = 200;

        // 小地图背景
        g.setColor(Color.BLACK);
        g.fillRect(mapX, mapY, mapSize, mapSize);
        g.setColor(Color.WHITE);
        g.drawRect(mapX, mapY, mapSize, mapSize);

        // 缩放比例
        double scaleX = mapSize / 780.0;
        double scaleY = mapSize / 780.0;

        // 绘制方块
        g.setColor(Color.GRAY);
        for (BlockData block : runtimeData.getBlockList()) {
            int x = mapX + (int)(block.getX() * scaleX);
            int y = mapY + (int)(block.getY() * scaleY);
            int w = (int)(block.getWidth() * scaleX);
            int h = (int)(block.getHeight() * scaleY);
            g.fillRect(x, y, Math.max(1, w), Math.max(1, h));
        }

        // 绘制大本营
        if (runtimeData.getCamp() != null) {
            g.setColor(runtimeData.getCamp().getState() == 1 ? Color.GREEN : Color.RED);
            int x = mapX + (int)(runtimeData.getCamp().getX() * scaleX);
            int y = mapY + (int)(runtimeData.getCamp().getY() * scaleY);
            int w = (int)(runtimeData.getCamp().getWidth() * scaleX);
            int h = (int)(runtimeData.getCamp().getHeight() * scaleY);
            g.fillRect(x, y, w, h);
        }

        // 绘制我方坦克
        if (runtimeData.getHero() != null && runtimeData.getHero().getState() == 1) {
            g.setColor(Color.YELLOW);
            int x = mapX + (int)(runtimeData.getHero().getX() * scaleX);
            int y = mapY + (int)(runtimeData.getHero().getY() * scaleY);
            g.fillRect(x, y, 4, 4);
        }

        // 绘制敌方坦克
        g.setColor(Color.CYAN);
        for (EnemyData enemy : runtimeData.getEnemyPool()) {
            if (enemy.getState() == 1) {
                int x = mapX + (int)(enemy.getX() * scaleX);
                int y = mapY + (int)(enemy.getY() * scaleY);
                g.fillRect(x, y, 3, 3);
            }
        }
    }
    // -------------------- view --------------------
}